package ar.edu.caece.pl.asem.model.impl.treeelements;

import ar.edu.caece.pl.asem.model.impl.SymbolTable;
import ar.edu.caece.pl.asem.temp.TemporalGenerator;

public class Arreglo extends SimboloGenerico {

	private int length; // Solo para arrays

	public Arreglo(String name, int type, int length) {
		super(name, type);
		this.length = length;
		this.label = "ARREGLO";
	}

	public int getLength() {
		return length;
	}

	public void setLength(int length) {
		this.length = length;
	}

	@Override
	public String toString() {

		return this.getLabel() + ": " + this.getName() + " ("
				+ SymbolTable.getVerboseType(this.getType()) + ") = "
				+ this.getLength();
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (!super.equals(obj))
			return false;
		if (getClass() != obj.getClass())
			return false;
		Arreglo other = (Arreglo) obj;
		if (length != other.length)
			return false;
		return true;
	}

	@Override
	public void generateASMCode() {
		StringBuilder sb = new StringBuilder();

		if (!(this.parent instanceof Declaraciones)) {
			TemporalGenerator.getInstance().generateTemporal();
			this.place = TemporalGenerator.getInstance().getActualTemporal();
			sb.append("\t mov ax, " + this.getChildren().get(0).getEnvName() + "_" + this.getChildren().get(0).getName() + " \n");
			sb.append("\t mov " + this.place + ",ax \n");
		}
		
		if(this.parent instanceof Declaraciones){
			String key = this.getEnvName() + "_" + this.getName();
				int size = this.getLength();
				StringBuffer def = new StringBuffer();
				for (int i = 0; i < size; i++) {
					def.append(" 0,");
				}
				sb.append(key + " dw "
						+ def.toString().substring(0, def.length() - 1)
						+ " \n");
		}
		
		this.code = sb.toString();

	}

}
